import React, {Component} from 'react';
import {NavLink} from "react-router-dom";
import axios from "../../utils/http"
import {Table, Button, Select, Input,message} from 'antd';
import './selectLog.css'
const { Option } = Select;

const columns = [
    {
        title: 'ID',
        dataIndex: 'logId',
    },
    {
        title: '操作者',
        dataIndex: 'operator',
    },
    {
        title: '操作时间',
        dataIndex: 'operaTime',
    },
    {
        title: 'IP地址',
        dataIndex: 'ipAdd',
    },
    {
        title: '操作记录',
        dataIndex: 'operaRec',
    },
];

let data = [];

// console.log(this.props.location.state.name )
//
export default class ManegerLog extends Component {

    constructor(props) {
        super(props);
        this.state = {
            selectedRowKeys: [], // Check here to configure the default column
            loading: false,
            logS:[],
        };
    }
    //===================================================进入页面时获取数据 ===========================
    componentDidMount(){


        function rTime(date) {
            var json_date = new Date(date).toJSON();
            return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        }
        // console.log(this.props.location.state.name.adminId )
        // console.log(data)
        axios.get('/mm-power/administrators/queryLogByAdminId',{
            params:{
                adminId:this.props.match.params.id
            }
        }).then(da=>{
            console.log( da.data.data)
            da.data.data.forEach((item,i)=>{

                item.operaTime=rTime(item.operaTime)
                item.key=item.logId

            })
            this.setState({
                logS: da.data.data
            })

        })
    }


    start = () => {
        this.setState({ loading: true });
        // ajax request after empty completing
        setTimeout(() => {
            this.setState({
                selectedRowKeys: [],
                loading: false,
            });
        }, 1000);
    };

    onSelectChange = selectedRowKeys => {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.setState({ selectedRowKeys });
    };

    //====================================================================删除方法===========================================
    delLog=()=>{
        console.log(this.state.selectedRowKeys)
        let arrList = [];
        for (let i in this.state.selectedRowKeys) {
            var j = {};
            j.logId = this.state.selectedRowKeys[i]
            arrList.push(j);
        }
        console.log(arrList)
        axios.post('/mm-power//administrators/deleteLogByLogIds',arrList

        ).then(data=>{
         if(data.data.code==200){
             message.success('删除成功')
             function rTime(date) {
                 var json_date = new Date(date).toJSON();
                 return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
             }
             // console.log(this.props.location.state.name.adminId )
             // console.log(data)
             axios.get('/mm-power/administrators/queryLogByAdminId',{
                 params:{
                     adminId:this.props.match.params.id
                 }
             }).then(da=>{
                 console.log( da.data.data)
                 da.data.data.forEach((item,i)=>{

                     item.operaTime=rTime(item.operaTime)
                     item.key=item.logId

                 })
                 this.setState({
                     logS: da.data.data
                 })

             })
         }else{
             message.error('删除失败')
         }
        })

    }
    render() {
        const { loading, selectedRowKeys } = this.state;
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };
        const hasSelected = selectedRowKeys.length > 0;
        function handleChange(value) {
            console.log(`selected ${value}`);
        }
        return (
            <div className='log'>

                <NavLink to='/Home/quanXian/Maneger'><span className='xiaoWei'>管理</span></NavLink>
             <span className='xiaoWei'>管理员日志</span>

                <br/>

            <br/>
                <Table rowSelection={rowSelection} columns={columns} dataSource={this.state.logS} />

                <Button type="primary" onClick={this.delLog} danger>删除</Button>
            </div>
        )
    }
}